//
//  BaseDetailAlphaNavVC.m
//  KuaiTu
//
//  Created by 江伟 on 2018/10/27.
//  Copyright © 2018年 WeiJiang. All rights reserved.
//

#import "BaseDetailAlphaNavVC.h"
#import "BaseImgTableHeader.h"
@interface BaseDetailAlphaNavVC ()
@property (nonatomic,strong) UIImage *alphaLeftImg;
@property (nonatomic,strong) UIColor *alphaTitleColor;
@property (nonatomic,strong) UIImage *alphaRightImg;
@property (nonatomic,strong) UIButton *alphaRightBtn;
@property (nonatomic,strong) UIImage *rightNormalImg;
@end

@implementation BaseDetailAlphaNavVC

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

/**
 设置导航栏右边按钮的文字
 */
- (void)setNavRightBtnWithTitle:(NSString*)title {
    //    self.navigationItem.rightBarButtonItem = [backBarItem shareRight:self:title];
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:title forState:UIControlStateNormal];
    rightBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    rightBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [rightBtn setFrame:CGRectMake(10, 0, 80, 40)];
    [rightBtn addTarget:self action:@selector(onTapNavRightBtn) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setTitle:title forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [rightBtn setTitleColor:[UIColor colorWithHexString:@"#212121"] forState:UIControlStateNormal];
    UIBarButtonItem *rightBarStarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightBarStarButtonItem;
    _alphaRightBtn = rightBtn;
}

/**
 设置导航栏右边按钮的图标
 */
-(void)setNavRightBtnWithImageName:(NSString*)imageName
{
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 60, 40);
    [btn setImage:IMG(imageName) forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(onTapNavRightBtn) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightBarStarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    self.navigationItem.rightBarButtonItem = rightBarStarButtonItem;
    _alphaRightBtn = btn;
    _rightNormalImg = IMG(imageName);
}

- (void)setupDragViewLeftImg:(UIImage *)leftImg titleColor:(UIColor *)titleColor rightImg:(UIImage *)rightImg {
    _alphaLeftImg = leftImg;
    _alphaTitleColor = titleColor;
    _alphaRightImg = rightImg;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    BaseImgTableHeader *header = (BaseImgTableHeader *)self.tableView.tableHeaderView;
    if ([header respondsToSelector:@selector(dragHeaderOffsetY:)]) {
        [header dragHeaderOffsetY:scrollView.contentOffset.y];
    }
    if (scrollView.contentOffset.y > header.height) {
        [self.navigationController.navigationBar setBackgroundImage:[[UIImage new] imageWithColor:[UIColor whiteColor]] forBarMetrics:UIBarMetricsDefault];
        if (self.alphaRightBtn && self.alphaRightImg) {
            [self.alphaRightBtn setImage:self.alphaRightImg forState:UIControlStateNormal];
        }
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:IMG(@"back") style:UIBarButtonItemStyleDone target:self action:@selector(back)];
        [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithHexString:@"#3a4254"]];
    } else {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        if (self.alphaRightBtn && self.rightNormalImg) {
            [self.alphaRightBtn setImage:self.rightNormalImg forState:UIControlStateNormal];
        }
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:IMG(@"back2") style:UIBarButtonItemStyleDone target:self action:@selector(back)];
        [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
